package cn.jiahua.hospital.service.impl;

import cn.jiahua.hospital.dto.PayHospitalDto;
import cn.jiahua.hospital.mapper.PayHospitalMapper;
import cn.jiahua.hospital.pojo.PayHospital;
import cn.jiahua.hospital.service.PayHospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.constants.Constants;

import javax.annotation.Resource;

@Service
public class PayHospitalServiceImpl implements PayHospitalService {

    @Resource
    private PayHospitalMapper payHospitalMapper;

    @Override
    public PayHospital getPayHospitalById(Integer id) {
        return payHospitalMapper.getPayHospitalById(id);
    }

    @Override
    public List<PayHospital> getPayHospitalListByMap(Map<String, Object> param) {
        return payHospitalMapper.getPayHospitalListByMap(param);
    }

    @Override
    public Integer getPayHospitalCountByMap(Map<String, Object> param) {
        return payHospitalMapper.getPayHospitalCountByMap(param);
    }

    @Override
    public Integer qdtxAddPayHospital(PayHospital payHospital) {
        payHospital.setPayHosptialCreatTime(new Date());
        return payHospitalMapper.insertPayHospital(payHospital);
    }

    @Override
    public Integer qdtxModifyPayHospital(PayHospital payHospital) {
        payHospital.setPayHosptialCreatTime(new Date());
        return payHospitalMapper.updatePayHospital(payHospital);
    }

    @Override
    public Integer qdtxDeletePayHospitalById(Integer id) {
        return payHospitalMapper.deletePayHospitalById(id);
    }

    @Override
    public Integer qdtxBatchDeletePayHospital(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return payHospitalMapper.batchDeletePayHospital(param);
    }

    @Override
    public Page<PayHospital> queryPayHospitalPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = payHospitalMapper.getPayHospitalCountByMap(param);
        pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<PayHospital> payHospitalList = payHospitalMapper.getPayHospitalListByMap(param);
        page.setList(payHospitalList);
        return page;
    }

    public Page<PayHospitalDto> getPayHospitalPage(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = payHospitalMapper.getPayHospitalCount((Integer) param.get("userId"));
        pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<PayHospitalDto> payHospital = payHospitalMapper.getPayHospital(param);
        page.setList(payHospital);
        return page;
    }
}